<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\member\controller;


use app\common\service\TicketService;
use think\Db;

/**
 * 优惠券控制器
 */
class Ticket extends Base
{
    public $noNeedLogin = [

    ];

    /**
     *   获取我的优惠券列表
     **/
    public function getList()
    {
        if ($this->request->isPost()) {
            $user_info = $this->user_info;
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;
            $query = Db::name('qt_ticket_send')
                ->alias('ts')
                ->field('ts.*,tk.id as tk_id ,tk.type,tk.name,tk.quota')
                ->leftJoin('qt_ticket tk','tk.id = ts.ticket_id')
                ->where("tk.store_id",$user_info['store_id'])
                ->where("ts.phone",$user_info['phone']);

            if (empty($post['type'])) {
                $this->result('状态不能为空','',204);
            }

            //已使用
            if ($post['type'] == 5) {
                $query->where('ts.is_receive',2);
            }

            //已使用
            if ($post['type'] == 1) {
                $query->where('ts.is_use',1);
            }

            //未使用
            if ($post['type'] == 2) {
                $query->where('ts.is_use',2)->where('ts.expiration_time','>=', time());
            }

            //已过期
            if ($post['type'] == 3) {
                $query->where('ts.expiration_time','<', time())->where([
                    ['ts.is_use','<>',1],
                    ['ts.is_delete','<>',1],
                    //['ts.is_receive','!=',1],
                ]);
            }

            //是否作废
            if ($post['type'] == 4) {
                $query->where('ts.is_delete',1);
            }

            $data = $query->order('ts.id desc')->paginate($limit)
                ->each(function ($item) {
                    $time = time();
                    //过期  时间小于当前时间，，为领取，或者为使用，或者为删除的
                    if ($time > $item['expiration_time'] && ( $item['is_use'] != 1 && $item['is_delete'] != 1)) {
                        $item['status'] = 1;//已过期
                    }else if ($item['is_delete'] == 1) {
                        $item['status'] = 2;//已作废
                    }else if ($item['is_use'] == 1) {
                        $item['status'] = 3;//已使用
                    }else if ($item['is_use'] == 2) {
                        $item['status'] = 4;//未使用
                    }/*else if ($item['is_receive'] == 1) {
                        $item['status'] = 5;//已领取
                    }*/else if ($item['is_receive'] == 2) {
                        $item['status'] = 6;//未领取
                    }

                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }else{
            $this->result('无效的请求', '', 204);
        }
    }


    /**
     * 领取优惠券
     */
    public function receive () {
        if($this->request->isPost()) {
            $send_id = $this->request->post('send_id');
            if (empty($send_id)) {
                $this->result('优惠券ID不能为空', '', 204);
            }
            $data = TicketService::getTicketSendById($send_id);
            if ($data['is_receive'] == 1) {
                $this->result('优惠券已经领取', '', 204);
            }

            Db::name('qt_ticket_send')->update([
                    'id'=>$send_id,
                    'is_receive'=>1,
                    'is_use'=>2,
                ]);
            $this->result('领取成功', '', 200);
        }

        $this->result('无效的请求', '', 204);
    }


}
